<!--
*Author：富行天下团队

 *代码由框架生成,任何更改都可能导致被代码生成器覆盖
 *业务请在@/extension/mall/Small_operate_manual.js此处编写
 -->
<template>
  <div>
    <VolBox
      :model.sync="model"
      :title="'手工录入--' + title"
      :width="400"
      :height="300"
    >
      <VolForm
        ref="myform"
        :label-width="100"
        :loadKey="true"
        :formFields="editFormFields"
        :formRules="editFormOptions"
      ></VolForm>
      <div slot="footer">
        <Button
          type="error"
          icon="md-checkmark"
          @click="submit"
          :loading="loading"
        >
          保存</Button
        >
        <Button type="info" icon="md-close" @click="model = false">关闭</Button>
      </div>
    </VolBox>
  </div>
</template>

<script>
import VolBox from "@/components/basic/VolBox.vue";
import VolForm from "@/components/basic/VolForm.vue";
import VolTable from "@/components/basic/VolTable.vue";

export default {
  components: {
    VolBox,
    VolForm,
    VolTable
  },
  data() {
    return {
      loading: false,
      title: "",
      model: false,
      editFormFields: {
        id: "",
        small_operate_manual_name_Id: "",
        dealName: "",
        dealNum: "",
        dealAmount: "",
        operateDate: "",
        dealStatus: ""
      },
      editFormOptions: [
        [
          {
            title: "商家名称",
            field: "dealName",
            type: "select",
            dataKey: "GetDataExpressCode",
            data: [],
            required: true,
            readonly: false
          }
        ],
        [
          {
            title: "日期",
            field: "operateDate",
            type: "date",
            required: true,
            readonly: false,
            onChange: val => {
              let c = this.dataList.some(
                i =>
                  this.editFormFields.dealName == i.dealName &&
                  val == i.operateDate
              );
              if (c) {
                this.editFormFields.operateDate = "";
                this.$Message.error("当前日期不可用");
              }
            }
          }
        ]
        [
          {
            title: "交易单数",
            field: "dealNum",
            type: "number",
            required: true
          }
        ],
        [
          {
            title: "交易金额",
            field: "dealAmount",
            type: "number",
            required: true
          }
        ],
      ],
      GetDataExpressCode: [],
      dataList: []
    };
  },
  methods: {
    getFormOption(field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    open(row) {
      if (row == undefined) {
        this.$refs.myform.reset();
        this.editFormOptions[0][0].readonly = false;
        this.editFormOptions[1][0].readonly = false;
      } else {
        this.editFormFields.id = row.id;
        this.editFormFields.small_operate_manual_name_Id =
          row.small_operate_manual_name_Id;
        this.editFormFields.dealName = row.dealName;
        this.editFormFields.dealNum = row.dealNum;
        this.editFormFields.dealAmount = row.dealAmount;
        this.editFormFields.operateDate = row.operateDate;
        this.editFormFields.dealStatus = row.dealStatus;
        this.editFormFields.operationStatus = row.operationStatus;
        this.editFormOptions[0][0].readonly = true;
        this.editFormOptions[1][0].readonly = true;
      }
      this.http.get("/api/Small_operate_xa/GetOperateManualName").then(res => {
        let dealName = this.getFormOption("dealName");
        dealName.data = res.map(i => {
          return {
            key: i.dealName,
            value: i.dealName
          };
        });
        this.GetDataExpressCode = res;
      });
    },
    submit() {
      this.$refs.myform.validate(() => {
        this.loading = true;
        this.editFormFields.small_operate_manual_name_Id = this.GetDataExpressCode.filter(
          i => {
            return i.dealName == this.editFormFields.dealName;
          }
        )[0].id;
        this.http
          .post(
            "/api/Small_operate_xa/AddOrUpdateOperateManual",
            this.editFormFields
          )
          .then(res => {
            this.loading = false;
            if (res.status) {
              this.model = false;
              this.$store.getters.data().viewPageTest.refresh();
              this.$Message.success("操作成功");
            } else {
              this.$Message.error(res.message);
            }
          });
      });
    }
  }
};
</script>
